package com.xbongbong.paas.gateway.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.gateway.pojo.vo.PlayBackVO;
import com.xbongbong.pro.config.ProBaseConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * http相关帮助类
 * @author 李少杰
 * @version v1.0.0
 * @date 2020/9/14 8:43
 * @since v1.0.0
 */
@Component
public class HttpHelp {

    private  final Logger log =  LoggerFactory.getLogger(this.getClass());

    @Resource
    private RestTemplate restTemplate;

    /**
     * 异步向流量回放服务器发送请求
     * @param url 请求url
     * @param playBackVO 封装的流量数据
     */
    @Async(value = "playBackThreadPool")
    public void sendPost4RestTemplate(String url, PlayBackVO playBackVO) {
        try {
            HttpHeaders header = new HttpHeaders();
            header.setContentType(MediaType.APPLICATION_JSON_UTF8);
            header.set("xbbToken", ProBaseConfig.getXbbPlayBackToken());
            HttpEntity<PlayBackVO> httpEntity = new HttpEntity<>(playBackVO, header);
            restTemplate.postForObject(url, httpEntity, JSONObject.class);
        } catch (Exception e) {
            log.error("HttpHelp 流量回放发送请求发生异常！", e);
        }
    }
}

